#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "ring_buffer.h"

static __attribute__((unused)) void ring_buffer_example1(ring_buffer_t *ring);
static __attribute__((unused)) void ring_buffer_example2(ring_buffer_t *ring);

int main(int argc, char **argv)
{
    /* init ring buffer struct */
    ring_buffer_t ring;
    ring_buffer_init(&ring);

    /* cite test examples */
    ring_buffer_example1(&ring);
    ring_buffer_example2(&ring);
}

static void ring_buffer_example1(ring_buffer_t *ring)
{
    uint8_t ch = 0;
    /* loop enqueue data into ring buffer */
    /* remember that ring buffer can only hold (RING_BUFFER_SIZE - 1) */
    for(ch = 0; ch < (128 - 1); ch++) {
        if(ring_buffer_enqueue(ring, ch) != 1) {
            printf("enqueue ch failed: 0x%x!\n", ch);
        }
    }
#if RING_BUFFER_DEBUG
    ring_buffer_debug(ring);
#endif /* RING_BUFFER_DEBUG */

    /* partly dequeue data from ring buffer */
    printf("\ndequeue data partly:\n(begin) -> ");
    for(int i = 0; i < 64; i++) {
        if(ring_buffer_dequeue(ring, &ch) != 1) {
            printf("null -> ");
        }
        else {
            printf("%d -> ", ch);
        }
    }
    printf("(end)\n");
#if RING_BUFFER_DEBUG
    ring_buffer_debug(ring);
#endif /* RING_BUFFER_DEBUG */

    /* fully dequeue data from ring buffer */
    printf("\ndequeue data fully:\n(begin) -> ");
    for(int i = 0; i < (256 - 1 - 64); i++) {
        if(ring_buffer_dequeue(ring, &ch) != 1) {
            printf("null -> ");
        }
        else {
            printf("%d -> ", ch);
        }
    }
    printf("(end)\n");
#if RING_BUFFER_DEBUG
    ring_buffer_debug(ring);
#endif /* RING_BUFFER_DEBUG */
}

static void ring_buffer_example2(ring_buffer_t *ring)
{
    /* enqueue array data into ring buffer */
    /* remember that ring buffer can only hold (RING_BUFFER_SIZE - 1) */
    uint8_t *ptr = (uint8_t *)"example: Hello World!";
    uint8_t pret[128] = { 0 };
    int n_read = 0;
    if(ring_buffer_enqueue_arr(ring, ptr, 22) != 22) {
        printf("enqueue array failed!\n");
    }

#if RING_BUFFER_DEBUG
    ring_buffer_debug(ring);
#endif /* RING_BUFFER_DEBUG */

    /* partly dequeue array data from ring buffer */
    n_read = ring_buffer_dequeue_arr(ring, pret, 8);
    if(n_read != 8) {
        printf("dequeue array failed: %d\n", n_read);
    }
    pret[n_read] = '\0';
    printf("\ndequeue array data partly:\n");
    printf("\"%s\"\n", pret);

#if RING_BUFFER_DEBUG
    ring_buffer_debug(ring);
#endif /* RING_BUFFER_DEBUG */

    /* fully dequeue array data from ring buffer */
    n_read = ring_buffer_dequeue_arr(ring, pret, 14);
    if(n_read != 14) {
        printf("dequeue array failed: %d\n", n_read);
    }
    pret[n_read] = '\0';
    printf("\ndequeue array data fully:\n");
    printf("\"%s\"\n", pret);

#if RING_BUFFER_DEBUG
    ring_buffer_debug(ring);
#endif /* RING_BUFFER_DEBUG */
}